#!/usr/bin/perl
#
# Change the "rollin" entries in the operations log to what's right.
# These were created because the original text log file did not record
# the operation type.
# Method:
# - Find some rollin entries
# - Get all log entries pertaining to them
# - Change the first to "new", and the remainder to "update"
# This does not work well when the date field is not filled in.
#
# PROBLEMS:
# Old deletes. They are an empty record. But the log rollin proc did not
# put a date on them, so they are undated. For now - skip.
#
use Counter;
use Getopt::Long;
use NoteHandler;

our $count = 10;
our $table = 'persons';
our $DEBUG = 0;
our $write = 1;
our $startkey = 1;

GetOptions("startkey=i" => \$startkey,
	   "count=i" => \$count,
	   "table=s" => \$table,
	   "debug" => \$DEBUG,
	   "write!" => \$write
	   );
# get a random filehandle for sql ops
my $stats = Counter::openfile("statistics");

my $n = new NoteHandler;
my $deletesfound;
for $key ($startkey..($startkey + $count - 1)) {
    $n->start($key);
    $DEBUG && print $table, " ", $key, "\n";
    my @changelist = $stats->selectany("* from f_log where tablename = ? and reckey = ? order by uniqueid", $table, $key . '');
    # important: coerce key to be string, or this goes real slow
    $DEBUG && print @changelist + 0, "  records:";
    my $op = 'new';
    my $reccount = 0;
    my $changecount = 0;
    my $undatedcount = 0;
    for $change (@changelist) {
	++ $reccount;
	$DEBUG && print " ", $change->{operation}, ":", $change->{date};
	# Check for undated records
	if (!$change->{date}) {
	    if ($change->{data} && $change->{data} !~ /^f_state:DELETED/) {
		$DEBUG && $n->note("Missing date after $reccount records");
	    } else {
		# this is actually a DELETE operation - but it's undated,
		# so don't change it (for now)
		++ $deletesfound;
		$DEBUG && $n->note("Empty (delete) logrec after $reccount records");
	    }
	    ++ $undatedcount;
	} elsif ($change->{operation} eq "rollin") {
	    if (!$change->{data}) {
		$n->note("Dated delete found");
		$op = 'delete';
	    }
	    $firstchange = $change->{date} if !$firstchange
		|| $firstchange gt $change->{date};
	    $lastchange = $change->{date} if !$lastchange
		|| $lastchange lt $change->{date};
	    if ($write) {
		my ($count, $sth) = $stats->dosql("update f_log set operation = ? where uniqueid = ? and operation = 'rollin'",
						  $op, $change->{uniqueid});
		if ($count != 1) {
		    die "Error in update\n";
		}
	    }
	    ++ $changecount;
	} else {
	    # no change needed - either nowrite or not rollin
	}
	$op = 'update';
    }
    $DEBUG && print "\nChanged $changecount records\n";
    $done{$rec->{reckey}} = 1;
    if ($undatedcount) {
	$n->done("Changed $changecount records, ignored $undatedcount");
    } else {
	$n->done("Changed $changecount records");
    }
    $totalupdates += $changecount;
}
$n->report();
print "\n";
print "Number of delete operations ignored: $deletesfound\n";
print "First change: ", $firstchange, "\n";
print "Last change : ", $lastchange, "\n";
print "Total number of updates: $totalupdates\n";
